package com.mitac.mcd.cs.mobiletracking.directoryserver;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;

import com.mitac.mcd.cs.mobiletracking.directoryserver.util.BusinessUtil;
import com.mitac.mcd.cs.mobiletracking.directoryserver.util.DBConnection;


public class MobileServer {

	public static String IP;
	public static int PORT;
	public ServerSocket server;
	private boolean _run = true;
	public static String ENCODING;
	public static final Properties ConfigProperties = new Properties();
    public static final Properties jdbcProperties = new Properties();
	
	public MobileServer() throws IOException{
		BusinessUtil.sLogger.info("******************************************************");
        BusinessUtil.sLogger.info("初始化系统参数开始...");
        String configFilePath = "./config.properties";//System.getProperty("user.dir") + "\\config.properties";
        BusinessUtil.sLogger.info("configFilePath:"+configFilePath);
        InputStream ConfigInputStream = new BufferedInputStream(new FileInputStream(configFilePath));
        //InputStream ConfigInputStream = getClass().getClassLoader().getResourceAsStream(proFilePath);
        ConfigProperties.load(ConfigInputStream);
        String jdbcFilePath = "./jdbc.properties";//System.getProperty("user.dir") + "\\jdbc.properties";
        BusinessUtil.sLogger.info("jdbcFilePath:"+jdbcFilePath);
        InputStream jdbcInputStream = new BufferedInputStream(new FileInputStream(jdbcFilePath));
        jdbcProperties.load(jdbcInputStream);
        //init db connection pool
        try {
			DBConnection.initAutConnection();
			DBConnection.initGPSDATAConnection();
		} catch (Exception e) {
			BusinessUtil.sLogger.error("数据库连接池初始化发生异常:"+e);
			BusinessUtil.sLogger.error("MobileServer启动失败...");
			return;
		}
		//获取到配置文件中设定的socket server IP
        IP = MobileServer.ConfigProperties.get("SOCKET_SERVER_IP").toString();
        BusinessUtil.sLogger.info("获取到配置文件中设定的socket server IP:"+IP);
        //获取到配置文件中设定的socket server PORT
        PORT = Integer.parseInt(MobileServer.ConfigProperties.get("SOCKET_SERVER_PORT").toString());
        BusinessUtil.sLogger.info("获取到配置文件中设定的socket server PORT:"+PORT);
        //获取配置文件中编码设置
        ENCODING = MobileServer.ConfigProperties.get("SOCKET_SERVER_ENCODING").toString();
        BusinessUtil.sLogger.info("获取配置文件中编码设置:"+ENCODING);
        
		listen();
	}
	
	public void listen() {
        try {
        	// Create the ServerSocket
			server = new ServerSocket(PORT);
			// Tell the world we're ready to go
	        BusinessUtil.sLogger.info( "Listening on :"+server );
	        // Keep accepting connections forever
	        while (_run) {
	            Socket socket = server.accept();
	            BusinessUtil.sLogger.info("Connection from "+socket);
	            new MobileServerThread(this, socket);
	        }
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
	
	public static void main(String args[]) throws IOException{
        new MobileServer();
    }
}
